<!DOCTYPE html>
<!-- This file tests JavaScript error messages of some of the
failure cases from http/tests/serviceworker/registration.html. It
should not be upstreamed to web platform tests since error messages
are not defined by the specification. -->
<title>Service Worker: Error messages for register()</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

function assert_register_fails(script, scope, message) {
  return navigator.serviceWorker.getRegistration(scope)
    .then(function(registration) {
        if (registration)
          return registration.unregister();
        return Promise.resolve();
      })
    .then(function() {
        return navigator.serviceWorker.register(script, { scope: scope});
      })
    .then(
        function() { assert_unreached('register should fail'); },
        function(error) { assert_equals(error.message, message); });
}

promise_test(function(t) {
    var script = 'resources/registration-worker.js';
    var scope = 'resources';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/resources\') with script (\'http://127.0.0.1:8000/serviceworker/resources/registration-worker.js\'): The path of the provided scope (\'/serviceworker/resources\') is not under the max scope allowed (\'/serviceworker/resources/\'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.';
    return assert_register_fails(script, scope, message);
  }, 'Registering same scope as the script directory without the last slash');

promise_test(function(t) {
    var script = 'resources/registration-worker.js';
    var scope = 'different-directory/';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/different-directory/\') with script (\'http://127.0.0.1:8000/serviceworker/resources/registration-worker.js\'): The path of the provided scope (\'/serviceworker/different-directory/\') is not under the max scope allowed (\'/serviceworker/resources/\'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.';
    return assert_register_fails(script, scope, message);
  }, 'Registration scope outside the script directory');

promise_test(function(t) {
    var script = 'resources/registration-worker.js';
    var scope = 'http://example.com';
    var message = 'Failed to get a ServiceWorkerRegistration: The origin of ' +
        'the provided documentURL (\'http://example.com\') does not match ' +
        'the current origin (\'' + window.location.origin + '\').';
    return assert_register_fails(script, scope, message);
  }, 'Registration scope outside domain');

promise_test(function(t) {
    var script = 'http://example.com/worker.js';
    var scope = 'http://example.com/scope/';
    var message = 'Failed to get a ServiceWorkerRegistration: The origin of ' +
        'the provided documentURL (\'http://example.com\') does not match ' +
        'the current origin (\'' + window.location.origin + '\').';
    return assert_register_fails(script, scope, message);
  }, 'Registering script outside domain');

promise_test(function(t) {
    var script = 'resources/no-such-worker.js';
    var scope = 'resources/scope/no-such-worker';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/resources/scope/no-such-worker\') with script (\'http://127.0.0.1:8000/serviceworker/resources/no-such-worker.js\'): A bad HTTP response code (404) was received when fetching the script.';
    return assert_register_fails(script, scope, message);
  }, 'Registering non-existent script');

promise_test(function(t) {
    var script = 'resources/invalid-chunked-encoding.php';
    var scope = 'resources/scope/invalid-chunked-encoding/';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/resources/scope/invalid-chunked-encoding/\') with script (\'http://127.0.0.1:8000/serviceworker/resources/invalid-chunked-encoding.php\'): An unknown error occurred when fetching the script.';
    return assert_register_fails(script, scope, message);
  }, 'Registering invalid chunked encoding script');

promise_test(function(t) {
    var script = 'resources/mime-type-worker.php';
    var scope = 'resources/scope/no-mime-type-worker/';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/resources/scope/no-mime-type-worker/\') with script (\'http://127.0.0.1:8000/serviceworker/resources/mime-type-worker.php\'): The script does not have a MIME type.';
    return assert_register_fails(script, scope, message);
  }, 'Registering script with no MIME type');

promise_test(function(t) {
    var script = 'resources/mime-type-worker.php?mime=text/plain';
    var scope = 'resources/scope/bad-mime-type-worker/';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/resources/scope/bad-mime-type-worker/\') with script (\'http://127.0.0.1:8000/serviceworker/resources/mime-type-worker.php?mime=text/plain\'): The script has an unsupported MIME type (\'text/plain\').';
    return assert_register_fails(script, scope, message);
  }, 'Registering script with bad MIME type');

promise_test(function(t) {
    var script = 'resources/redirect.php?Redirect=' +
                  encodeURIComponent('..//resources/registration-worker.js');
    var scope = 'resources/scope/redirect/';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/resources/scope/redirect/\') with script (\'http://127.0.0.1:8000/serviceworker/resources/redirect.php?Redirect=..%2F%2Fresources%2Fregistration-worker.js\'): The script resource is behind a redirect, which is disallowed.';
    return assert_register_fails(script, scope, message);
  }, 'Registering redirected script');

promise_test(function(t) {
    var script = 'resources/malformed-worker.php?parse-error';
    var scope = 'resources/scope/parse-error';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/resources/scope/parse-error\') with script (\'http://127.0.0.1:8000/serviceworker/resources/malformed-worker.php?parse-error\'): ServiceWorker script evaluation failed';
    return assert_register_fails(script, scope, message);
  }, 'Registering script including parse error');

promise_test(function(t) {
    var script = 'resources/malformed-worker.php?undefined-error';
    var scope = 'resources/scope/undefined-error';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/resources/scope/undefined-error\') with script (\'http://127.0.0.1:8000/serviceworker/resources/malformed-worker.php?undefined-error\'): ServiceWorker script evaluation failed';
    return assert_register_fails(script, scope, message);
  }, 'Registering script including undefined error');

promise_test(function(t) {
    var script = 'resources/malformed-worker.php?uncaught-exception';
    var scope = 'resources/scope/uncaught-exception';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/resources/scope/uncaught-exception\') with script (\'http://127.0.0.1:8000/serviceworker/resources/malformed-worker.php?uncaught-exception\'): ServiceWorker script evaluation failed';
    return assert_register_fails(script, scope, message);
  }, 'Registering script including uncaught exception');

promise_test(function(t) {
    var script = 'resources/malformed-worker.php?import-malformed-script';
    var scope = 'resources/scope/import-malformed-script';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/resources/scope/import-malformed-script\') with script (\'http://127.0.0.1:8000/serviceworker/resources/malformed-worker.php?import-malformed-script\'): ServiceWorker script evaluation failed';
    return assert_register_fails(script, scope, message);
  }, 'Registering script importing malformed script');

promise_test(function(t) {
    var script = 'resources/malformed-worker.php?import-no-such-script';
    var scope = 'resources/scope/import-no-such-script';
    var message = 'Failed to register a ServiceWorker for scope (\'http://127.0.0.1:8000/serviceworker/resources/scope/import-no-such-script\') with script (\'http://127.0.0.1:8000/serviceworker/resources/malformed-worker.php?import-no-such-script\'): ServiceWorker script evaluation failed';
    return assert_register_fails(script, scope, message);
  }, 'Registering script importing non-existent script');

promise_test(function(t) {
    var script = 'resources%2fempty-worker.js';
    var scope = 'resources/scope/encoded-slash-in-script-url';
    var message = 'Failed to register a ServiceWorker: The provided scope ' +
        '(\'' + window.location.origin + '/serviceworker/resources/scope/' +
        'encoded-slash-in-script-url\') or scriptURL ' +
        '(\'' + window.location.origin + '/serviceworker/' +
        'resources%2fempty-worker.js\') ' +
        'includes a disallowed escape character.';
    return assert_register_fails(script, scope, message);
  }, 'Script URL including URL-encoded slash');

</script>
